package com.navi.loaddata.core.etlException;

/**
 * Created by ghost on 2018/6/1.
 */

import com.navi.loaddata.core.errorLog.ErrorCode;

/** 这里继承RuntimeException异常 **/
public class ETLException2 extends RuntimeException {
    private static final long serialVersionUID = 2332608236621015980L;
    /** 错误码 **/
    private ErrorCode errorCode;
    private Throwable mainCause;
    private String glassMsg;

    //Exception
    public ETLException2(Throwable cause) {
        super(cause);
    }
    //ERRORCode
    public ETLException2(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    //ERRORcode  Exception
    public ETLException2(ErrorCode errorCode, Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        //特殊异常处理
        if(null!=cause) {
            if (cause.toString().contains("ORA-01683") || cause.toString().contains("ORA-01688")) {
                this.errorCode = ErrorCode.ERROR_ORA_TEMP_TABLESPACE;
            } else if (cause.toString().contains("Unable to acquire JDBC Connection")) {
                this.errorCode = ErrorCode.ERROR_SQL_CONNEECTION;
            } else if (cause.toString().contains("connect timed out")) {
                this.errorCode = ErrorCode.ERROR_CONNEECTION_TIME_OUT;
            }else if(cause.toString().contains("Unexpected internal error")){
                this.errorCode=ErrorCode.ERROR_GP_OUT_OF_MERMORY;
            }else if(cause.toString().contains("Too many connections")){
                this.errorCode=ErrorCode.ERROR_TOO_MANY_CONNEECTION;
            }
        }
        this.mainCause=cause;
    }
    //glassId,ERRORcode  Exception
    public ETLException2(String glass,ErrorCode errorCode, Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.glassMsg = glass;
        //特殊异常处理
        if(null!=cause) {
            if (cause.toString().contains("ORA-01683") || cause.toString().contains("ORA-01688")) {
                this.errorCode = ErrorCode.ERROR_ORA_TEMP_TABLESPACE;
            } else if (cause.toString().contains("Unable to acquire JDBC Connection")) {
                this.errorCode = ErrorCode.ERROR_SQL_CONNEECTION;
            } else if (cause.toString().contains("connect timed out")) {
                this.errorCode = ErrorCode.ERROR_CONNEECTION_TIME_OUT;
            }else if(cause.toString().contains("Unexpected internal error")){
                this.errorCode=ErrorCode.ERROR_GP_OUT_OF_MERMORY;
            }else if(cause.toString().contains("Too many connections")){
                this.errorCode=ErrorCode.ERROR_TOO_MANY_CONNEECTION;
            }
        }
        this.mainCause=cause;
    }

    public ErrorCode getErrorCode() {
        return this.errorCode;
    }

    public void setErrorCode(ErrorCode errorCode) {
        this.errorCode = errorCode;
    }


    public Throwable getMainCause() {
        if(null==this.mainCause) return null;
        return getErrCauseBy(this.getCause());
    }

    public String getGlassMsg() {
        return glassMsg;
    }

    public void setGlassMsg(String glassMsg) {
        this.glassMsg = glassMsg;
    }

    private Throwable getErrCauseBy(Throwable cause) {
        if(null==cause) return null;

        //获得底层CauseBy
        Throwable causeBy=cause;
        while(causeBy.getCause()!=null){
            causeBy=causeBy.getCause();
        }
        return causeBy;
    }

    public String getErrMsg(){
        if (null==this.mainCause) return this.getErrorCode().getErrorMsg();
        return this.getMainCause().toString();
    }



}



